#!/bin/sh
set -uxe

llt_dir=/usr1/LLT
export HOME="/root"
CUR_DIR=$(cd `dirname $0`;pwd)
mkdir -p $llt_dir

OBSSERVER_IP="100.109.85.100"
set +u
if [ "x${obs_server_from_jenkins}" != "x" ]; then
    OBSSERVER_IP="${obs_server_from_jenkins}"
fi
set -u

function check_env()
{
cat << END1 > /root/.oscrc
[general]
apiurl = https://euleros-obs
no_verify = 1

[https://euleros-obs]
user = root
pass = EulerOS_123
END1

    if [ "$OBSSERVER_IP" == "100.109.85.100" ];then
        OBSUI_IP="100.109.85.108"
    else
        OBSUI_IP="$OBSSERVER_IP"
    fi

    sed -i '/euleros-obs/d' /etc/hosts
    echo "$OBSSERVER_IP euleros-obs-repo.huawei.com euleros-obs-repo" >> /etc/hosts
    echo "$OBSUI_IP euleros-obs.huawei.com euleros-obs" >> /etc/hosts
    rm -f /root/.config/osc/trusted-certs/euleros-obs_443.pem
expect -c "
    set timeout -1
    spawn osc ls SUSE:SLE-11:SP3
    expect {
        \"Enter choice*\" {send \"2\r\" ; exp_continue }
        eof { catch wait result; exit [lindex \$result 3] }
     }
     expect {
         eof { catch wait result; exit [lindex \$result 3] }
     }
"
    if [ $? -ne 0 ]; then
        echo "FAILED: WHEN osc ls SUSE:SLE-11:SP3"
        exit 1
    fi
    [ -d $llt_dir ] || mkdir -p $llt_dir
}

function checkout_package()
{
    pro="$1"
    pack="$2"
    rm -rf "$pro/$pack"
    set +e
    osc co "$pro" "$pack"
    if [ $? -ne 0 ]; then
        sleep 60
        osc co "$pro" "$pack"
        if [ $? -ne 0 ]; then
            sleep 90
            osc co "$pro" "$pack"
            if [ $? -ne 0 ]; then
                echo "ERROR: osc co $pro $pack failed"
                exit 1
            fi
        fi
    fi
    set -e
}

function rename_obsfile()
{
    dir=$1
    pushd $dir
    set +e
    if [ -f '_service' ]; then
        osc up -S
        if [ $? -ne 0 ]; then
            sleep 60
            osc up -S
            if [ $? -ne 0 ]; then
                sleep 90
                osc up -S
                if [ $? -ne 0 ]; then
                    echo "Failed: osc up -S"
                    exit 1
                fi
            fi
        fi
    fi
    set -e

    for file in `ls`
    do 
        [[ -d $file ]] && continue
        if [ "$file" != "_service" ]; then
            newfile=${file##_service*:}
            mv $file $newfile
        fi
    done
    popd
}

function modify_spec()
{
    dir=$1
    speclist=`ls $dir/*.spec`
    for file in $speclist
    do
        sed -i '/^[Bb]uild[Rr]oot/a\BuildRequires:  vim perl-GD gcc procps grep gawk perl-Digest-MD5' $file
    done
}

function create_buildroot()
{
    #clean obs cache
    rm -rf /var/tmp/osbuild-packagecache
    echo "clean obs cache:  rm -rf /var/tmp/osbuild-packagecache"
    prj=$1
    pkg=$2
    dir=$3
    mkdir -p $dir
    pushd $prj/$pkg
expect -c "
     set timeout -1
     spawn osc build standard_x86_64 --root $dir
     expect {
         \"*trust packages just this time*\" {send \"1\r\";exp_continue}
         eof { catch wait result; exit [lindex \$result 3] }
     }
     expect {
         eof { catch wait result; exit [lindex \$result 3] }
     }
"
    if [ $? -ne 0 ]; then
        echo "Failed: osc build --root $dir"
        clean_build_root $dir
        exit 1
    fi
    popd
}

function clean_build_root_exit()
{
    status=$?
    dir=$1
    if [ $status -ne 0 ]; then
        set +e
        umount $dir/proc
        set -e
        rm -rf $dir
    fi
}

function fun_main()
{
    if [ $# -ne 2 ];then
        echo "Usage: sh create_llt_env.sh <project> <pkg>"
        exit 1
    else
        project=$1
        pkg=$2
    fi
    
    if [ -d $llt_dir/$project/$pkg/build-root ];then
        echo "compile env exits"
        cd $llt_dir/$project/$pkg
        osc up
        exit 0
    fi
    
    pushd $llt_dir
    build_root="$llt_dir/$project/$pkg/build-root"
    trap "clean_build_root_exit $build_root" EXIT
    check_env
    checkout_package "$project" "$pkg"
    rename_obsfile "$project/$pkg"
    modify_spec "$project/$pkg"
    create_buildroot $project $pkg $build_root
    popd
    cp $CUR_DIR/gensummary $build_root/usr/bin/
    chmod +x $build_root/usr/bin/gensummary
    cp $CUR_DIR/lcov-1.11-29.1.noarch.rpm $build_root
cat >test.cfg <<L_EOF
set +e
ls /usr/bin/lcov
ret=\$?
set -e
if [ \$ret -ne 0 ];then
    rpm -ivh lcov-1.11-29.1.noarch.rpm
fi
    rm -rf lcov-1.11-29.1.noarch.rpm
L_EOF
    chroot $build_root < test.cfg
}

fun_main $@
